package com.dudsbury.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.ChangedCharSetException;
import javax.swing.text.DefaultCaret;
import javax.swing.JScrollPane;
import java.awt.FlowLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TutorialFrame extends JFrame {

	private JPanel contentPane;
	JTextPane tPane;
	JScrollPane scroll;
	
	JFrame thisFrame = this;

	public TutorialFrame() throws IOException {
		setTitle("Tutorial");
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(1080, 640);
		
		
		setBounds(150, 25, 1080, 690);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		tPane = new JTextPane();
		
		//JScrollPane scrollPane = new JScrollPane(tPane);

		//contentPane.add(scrollPane);

		
		DefaultCaret caret = (DefaultCaret) tPane.getCaret();
		caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
		
		//FileReader reader = new FileReader(
			//	"trunk/dudsbury/src/res/res/TUT.htm");

		String s = readFileAsString("trunk/dudsbury/src/res/TUT.htm");
		//System.out.println(s);
		// 
		tPane.setContentType("text/html");
		

		//DefaultCaret caret = (DefaultCaret) tPane.getCaret();
		//caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
		
		
		//System.out.println(tPane.getText());
		System.out.println(new File(".").getCanonicalPath());
		for (int i = 1; i < 20; i++) {

			try {
			String path = this.getClass().getClassLoader()
					   .getResource("res/a"+i+".png").toString();
			//System.out.println(path);
			//path = ("trunk/dudsbury/src/res/TUT_files/(" + i + ").png");

			//String rep = String.format"~~~",path);
			s = s.replaceAll("~~~" + i + ".png", path);
			}catch (Exception e) {
				System.out.println("Pic Not Found");
			}
			//System.out.println("FILE: " + s);
			// TUT_files/~~~~20.png
		}

		tPane.setEditable(false);
		tPane.setText(s);
		
		try {

			// tPane.read(reader, null);
		} catch (Exception e) {
			System.out.println("ERROR: " + e);
		}
		scroll = new JScrollPane(tPane);
		scroll.setPreferredSize(new Dimension(800, 600));
		
		JButton btnClose = new JButton("Exit");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				thisFrame.dispose();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(132)
					.addComponent(scroll, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(122, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(503, Short.MAX_VALUE)
					.addComponent(btnClose)
					.addGap(500))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(5)
					.addComponent(scroll, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnClose)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		scroll.setColumnHeaderView(panel);
		
		JLabel lblDudsburyTutorial = new JLabel("Dudsbury Tutorial");
		lblDudsburyTutorial.setFont(new Font("Arial", Font.BOLD, 18));
		panel.add(lblDudsburyTutorial);
		contentPane.setLayout(gl_contentPane);

	
	}

	public static String readFileAsString(String filePath)
			throws java.io.IOException {
		StringBuffer fileData = new StringBuffer(1000);
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		char[] buf = new char[1024];
		int numRead = 0;
		while ((numRead = reader.read(buf)) != -1) {
			String readData = String.valueOf(buf, 0, numRead);
			fileData.append(readData);
			buf = new char[1024];
		}
		reader.close();
		return fileData.toString();
	}
}
